package net.khoroshev.wschat.model;

import java.beans.Transient;

import javax.servlet.http.HttpSession;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;



public class SessionWrapper {
    private transient HttpSession session;
    /**
     * @return
     * @see javax.servlet.http.HttpSession#getId()
     */
    public String getId() {
        return session.getId();
    }

    public SessionWrapper(HttpSession session) {
        this.session = session;
    }

    /**
     * @return the user name
     */
    public String getUserName() {
        String result = null;
        Authentication auth = auth();
        if (auth != null) {
            result = auth.getName();
        }
        return result;
    }
    @Transient
    private Authentication auth() {
        Authentication auth = null;
        SecurityContext context = (SecurityContext) session.getAttribute("SPRING_SECURITY_CONTEXT");
        if (context != null) {
            auth = context.getAuthentication();
        }
        return auth;
    }
}